package com.imagefake.config.handler;

import com.alibaba.fastjson2.JSON;
import com.imagefake.vo.HttpResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AppAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //设置响应编码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        //返回JSON出去
        HttpResult httpResult=new HttpResult(-1,"您没有权限访问");
        //把httpResult转成JSON
        String httpResultJSON = JSON.toJSONString(httpResult);
        response.getWriter().write(httpResultJSON);
        response.getWriter().flush();

    }
}
